package tn.orange.canreminder.adapter;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import tn.orange.canreminder.R;
import tn.orange.canreminder.model.Competition;
import tn.orange.canreminder.model.Country;
import tn.orange.canreminder.model.Phase;
import tn.orange.canreminder.model.Poule;
import tn.orange.canreminder.singleton.CountrySingleton;
import tn.orange.canreminder.singleton.PhaseSingleton;
import tn.orange.canreminder.singleton.PouleSingleton;
import tn.orange.canreminder.utils.DateUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MatchAdapter extends BaseAdapter {

	private Context context;
	private List<Competition> competitions;
	private LayoutInflater mInflater;

	public MatchAdapter(Context context, List<Competition> competitions) {
		super();
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.competitions = competitions;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return competitions.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return competitions.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {

		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.match_item, null);
			holder = new ViewHolder();
			holder.nomPays1 = (TextView) convertView
					.findViewById(R.id.match_item_pays1);
			holder.nomPays2 = (TextView) convertView
					.findViewById(R.id.match_item_pays2);
			holder.scorefinal1 = (TextView) convertView
					.findViewById(R.id.match_item_score1_final);
			holder.scorefinal2 = (TextView) convertView
					.findViewById(R.id.match_item_score2_final);
			holder.img1 = (ImageView) convertView
					.findViewById(R.id.match_item_img1);

			holder.conf = (ImageView) convertView
					.findViewById(R.id.imageButton1);

			holder.fb = (ImageView) convertView.findViewById(R.id.login);
			holder.img2 = (ImageView) convertView
					.findViewById(R.id.match_item_img2);

			holder.score1_pro = (TextView) convertView
					.findViewById(R.id.match_item_score1_pro_final);
			holder.score2_pro = (TextView) convertView
					.findViewById(R.id.match_item_score2_pro_final);

			holder.score1_pen = (TextView) convertView
					.findViewById(R.id.match_item_score_pen_final1);
			holder.score2_pen = (TextView) convertView
					.findViewById(R.id.match_item_score_pen_final2);

			holder.poule = (TextView) convertView
					.findViewById(R.id.match_item_group);

			holder.date = (TextView) convertView
					.findViewById(R.id.match_item_date);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Competition competition = competitions.get(position);

		holder.conf.setTag(competition);

		// Verification Poule Ou Match eliminatoire

		if (competition.isIspoulecompetition()) {

			convertView.findViewById(R.id.prolongation).setVisibility(
					LinearLayout.GONE);
			convertView.findViewById(R.id.penalty).setVisibility(
					LinearLayout.GONE);

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					80, 80);
			params.bottomMargin = 5;
			holder.scorefinal1.setTextSize(24);
			holder.scorefinal2.setTextSize(24);
			convertView.findViewById(R.id.match).setLayoutParams(params);

			int pouleId = competition.getPoule_id();
			holder.poule.setText("" + getPoulebyId(pouleId));
		} else
			holder.poule.setText(getPhaseById(competition.getPhase_id()));

		// verification date
		String date = DateUtils.getDate(competition.getCompetitiondate());

		holder.date.setText(date);

		// Verification du score

		if (competition.getScorefinal1() != -1) {
			holder.scorefinal1.setText("" + competition.getScorefinal1());
			holder.scorefinal2.setText("" + competition.getScorefinal2());
		}

		if (competition.isIseleminationcompetition()) {

			if (competition.getScorefinalpro1() != -1) {
				holder.score1_pro.setText("" + competition.getScorefinalpro1());
				holder.score2_pro.setText("" + competition.getScorefinalpro2());
			} else {
				convertView.findViewById(R.id.prolongation).setVisibility(
						LinearLayout.GONE);
				convertView.findViewById(R.id.penalty).setVisibility(
						LinearLayout.GONE);

				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						80, 80);
				params.bottomMargin = 5;
				convertView.findViewById(R.id.match).setLayoutParams(params);
			}

			if (competition.getScorepenalty1() != -1) {
				holder.score1_pen.setText("" + competition.getScorepenalty1());
				holder.score2_pen.setText("" + competition.getScorepenalty2());

			} else {
				holder.score1_pen.setText(" ");
				holder.score2_pen.setText(" ");
				convertView.findViewById(R.id.penalty).setVisibility(
						LinearLayout.GONE);

			}
		}
		// verification des noms des pays

		List<Country> countries = CountrySingleton.getInstance();
		Country country1 = getCountrybyId(competition.getCountryone_id(),
				countries);
		Country country2 = getCountrybyId(competition.getCountrytwo_id(),
				countries);
		String name1 = country1.getName();
		String name2 = country2.getName();

		holder.fb.setTag(name1 + " VS " + name2 + " le "
				+ competition.getCompetitiondate());

		holder.nomPays1.setText(name1);
		holder.nomPays2.setText(name2);

		// Verification des images
		String slug1 = country1.getNameslug();
		String slug2 = country2.getNameslug();

		holder.img1.setImageResource(getIdFromSlug(slug1));
		holder.img2.setImageResource(getIdFromSlug(slug2));
		return convertView;
	}

	static class ViewHolder {
		TextView nomPays1;
		TextView nomPays2;
		TextView score1_pro;
		TextView score2_pro;
		TextView score1_pen;
		TextView score2_pen;
		TextView scorefinal1;
		TextView scorefinal2;
		ImageView img1;
		ImageView img2;
		TextView poule;
		TextView date;
		ImageView conf;
		ImageView fb;

	}

	private String getPoulebyId(int id) {
		List<Poule> poules = PouleSingleton.getInstance();
		for (Poule poule : poules) {
			if (poule.getId() == id)
				return poule.getName();
		}
		return "";
	}

	private Country getCountrybyId(int id, List<Country> countries) {
		for (Country country : countries) {
			if (country.getId() == id)
				return country;
		}
		return new Country();
	}

	public static int getIdFromSlug(String slug) {
		slug = slug.replace("-", "");
		try {
			return R.drawable.class.getField(slug).getInt(null);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	private String getPhaseById(int id) {

		List<Phase> phases = PhaseSingleton.getInstance();
		for (Phase phase : phases) {
			if (phase.getId() == id)
				return phase.getName();
		}
		return "";
	}

	private String getFirstName(String name) {
		int pos = name.indexOf(" ");
		if (pos != -1)
			return name.substring(0, pos);
		else
			return name;
	}

}
